import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

//为了方便测试，此处用vuex做全局数据
const store = new Vuex.Store({
	state: {
		userInfo: {},
		isLogin: false,
		isAuth:false,
		orderType: 'takein',
		addresses: [],
		defaultAddress:{},
		remark: '不打包',
		cart:{},
		ordersCache:[],
		swipers:[]
	},
	mutations: {
		SET_CARSOUSEL(state,carousels){
			state.swipers=carousels;
		},
		SET_ORDER(state,orders){
			state.ordersCache=orders
		},
		SET_AUTH(state,auth){
			state.isAuth=auth;
		},
		SET_ADDRESS(state, address) {
			let defaultAddress=address.find(data=>data.isDefault==1);
			if(defaultAddress){
				state.defaultAddress=defaultAddress
			}else{
				state.defaultAddress={};
			}
			state.addresses = address
		},
		SET_REMARK(state, remark) {
			state.remark = remark
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo
		},
		SET_ISLOGIN(state, isLogin) {
			state.isLogin = isLogin
		},
		SET_CART(state,cart){
			state.cart=cart
		},
		SET_DEFAULT_ADDRESS(state,address){
			state.defaultAddress=address
		}
	
	},
	getters:{
		IS_AUTH:state=>state.IS_AUTH,
		USERINFO:state=>state.userInfo
	}
})

export default store
